
/*---------------------------------------------------------------------------

  FILENAME:
        serial2sock.c

  PURPOSE:
	diliver data from serial to different sockets.

  REVISION HISTORY:
        Date            Engineer        Revision        Remarks
	16/02/2009	Vincent. Young	1.2	

  NOTES:


  LICENSE:
        Copyright (c) 2009, Vincent Young (vincentgodyoung@hotmail.com)

        This source code is released for free distribution under the terms
        of the GNU General Public License.

----------------------------------------------------------------------------*/

// local project include files
#include "serial.h"
#include "socket.h"
#include "serial2sock.h"

// system library include files
#include <stdio.h>
#include <errno.h>
#include <string.h>

#define LOGREGMODE FALSE

void * serial2sock(int * port)
{
	char buf[MAXSIZE];
	int len = 0;
	while(1)
	{
		memset(buf, '\0', MAXSIZE);
		// 从串口取数据
		len = SerialNonBlockRead(buf, MAXSIZE);
		if (len < 0)
		{
			printf("serial2sock: cannot read serial [%d]\n", len);
			return NULL;
		}
		else if (len == 0)
		{
#if LOGREGMODE
			//printf("serial2sock: received nothing, continueing.......\n");
#endif
			continue;
		}
		// 分拣出不同的去向socketid
		// 发送到相应的socketid
#if LOGREGMODE
		printf("serial2sock: its sending to socketid[%d] length[%d] string[%s]\n", 
			socketid, len, buf);
#endif
		len = TCPWrite(socketid, buf, len);
		if (len < 0)
		{
			printf("serial2sock: cannot send to socket [%d]\n", socketid);
			//return NULL;		// no need to exit
			continue;
		}
#if	LOGREGMODE
		printf("serial2sock: send to socket [%d] [%s]\n", socketid, buf);
#endif
	}
#if	LOGREGMODE
	printf("serial2sock: never coming here!\n");
#endif
	return NULL;
}


